package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bean.student;
import sql.sqlConnect;
/*
 * 学生的业务逻辑
 */
public class studentDao {
	//输入学生注册信息
	public Connection con = null;
	public void insertStudent(student student){
		String sqlStr = "insert into student(student_num,name,sex,grade,profession,username,password)" +
				" values ('" + student.getStudentNum() + "','" + student.getName() + "'," +student.getSex()+ "," + student.getGrade() + 
				",'"+student.getProfession()+"','"+student.getUsername()+"','"+student.getPassword()+"'"+");";
		System.out.println(sqlStr);
		try {
				
			PreparedStatement pre = con.prepareStatement(sqlStr);
			pre.execute();
		} catch (SQLException e) {
				e.printStackTrace();
		}	
	}
	
	//获得学生信息
	public List<student> selectAllStudent (){
		String sqlStr = "select * from student where 1=1";
		try {
			PreparedStatement ps = con.prepareStatement(sqlStr);
			ResultSet rs = ps.executeQuery();
			List<student> studentList = new ArrayList<student>();
			while(rs.next()){
				student student = new student();
				student.setStudentNum(rs.getString(1));
				student.setName(rs.getString(2));
				student.setSex(rs.getInt(3));
				student.setGrade(rs.getInt(4));
				student.setProfession(rs.getString(5));
				student.setUsername(rs.getString(6));
				student.setPassword(rs.getString(7));
				studentList.add(student);
			}
			return studentList;
		} catch (SQLException e) {
			System.out.println("查无结果");
			e.printStackTrace();
			return null;
		}	
	}
}
